package com.huaxin.hxcloud.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Entity.HxMaintenanceDTO;
import com.huaxin.common.Entity.HxMaintenanceFirstDTO;
import com.huaxin.common.Global;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.utils.HxUtils;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.common.view.HxDateView;
import com.huaxin.hxcloud.R;
import com.huaxindata.common.util.HxLogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2018/6/6
 * @Desc SPT运维分析树形结构
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxSPTMaintenanceActivity extends BaseActivity {

    ArrayList<MultiItemEntity> data = new ArrayList<>();
    @BindView(R.id.more)
    TextView     mMore;
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.dataview)
    HxDateView   mDataview;
    @BindView(R.id.rv_station_target)
    RecyclerView mRvStationTarget;
    @BindView(R.id.content)
    FrameLayout  mContent;

    private HxSPTMaintenanceAdapter mAdapter;
    private View                    mEmptyView;
    private static final String TAG = "HxSPTMaintenanceActivit---->";
    private String       mSelectDate;
    private List<String> mSelectList=new ArrayList<>();
    private boolean mIsSelect = false;

    @Override
    protected int getContentViewId() {
        return R.layout.act_spt_maintenance;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(mToolbar);
        mToolbar.setTitle(Global.user.getOrgName() + "");
        initWebView();
        mAdapter = new HxSPTMaintenanceAdapter(data);
        mRvStationTarget.setAdapter(mAdapter);
        mRvStationTarget.setLayoutManager(new LinearLayoutManager(this));
        //空布局
        mEmptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) mRvStationTarget.getParent(), false);
        mDataview.setOnSelectDateListener(new HxDateView.onSelectDateListener() {
            @Override
            public void setSelectDate(String data, boolean isSelect) {
                mSelectDate = data;
                mIsSelect = isSelect;
            }
        });
        mMore.setText("确定");
        mAdapter.setOnSelectListListener(new HxSPTMaintenanceAdapter.onSelectListListener() {
            @Override
            public void getSelectList(List<String> selects) {
                HxLogUtils.i(TAG + "选择集合" + selects);
                mSelectList = selects;
            }
        });
    }

    @OnClick({R.id.more})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more:
                //运维分析图表
                if (mSelectList.size() > 0) {
                    Intent intent = new Intent(this, HxSPTStatisticsActivity.class);
                    intent.putStringArrayListExtra("MAINTENACE_LIST", (ArrayList<String>) mSelectList);
                    String flagDate;
                    if (mIsSelect) {
                        flagDate = mSelectDate;
                    } else {
                        flagDate = HxUtils.getYearMonth();
                    }
                    intent.putExtra("MAINTENACE_DATE", flagDate);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast("至少选择一个区域");
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void requestData() {
        super.requestData();
        updateLoadStatus(LOADING);
        Call<HxMaintenanceDTO> call = HxTransmitRequestHelper.getRequestApi().getSPTOrgTree();
        autoClose(call);
        call.enqueue(new Callback<HxMaintenanceDTO>() {
            @Override
            public void onResponse(Call<HxMaintenanceDTO> call, Response<HxMaintenanceDTO> response) {
                HxMaintenanceDTO maintenanceDTO = response.body();
                if (maintenanceDTO != null && maintenanceDTO.isSuccessful()) {
                    List<HxMaintenanceFirstDTO> children = maintenanceDTO.getData().getChildren();
                    HxLogUtils.i(TAG + children.size() + "---->" + maintenanceDTO.toString());
                    if (children != null && children.size() > 0) {
                        updateLoadStatus(LOAD_SUCCESS);
                        setDataView(children);
                    } else {
                        mAdapter.setEmptyView(mEmptyView);
                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<HxMaintenanceDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
                HxLogUtils.i(TAG + t);
            }
        });
    }

    /**
     * 初始化数据
     *
     * @param childrenBeans
     */

    private void setDataView(List<HxMaintenanceFirstDTO> childrenBeans) {
        data.clear();

        for (int i = 0; i < childrenBeans.size(); i++) {
            HxMaintenanceFirstDTO maintenanceFirstDTO = childrenBeans.get(i);
            HxLogUtils.i(TAG + "--data--" + maintenanceFirstDTO.toString());
            //默认设置未选择状态
            maintenanceFirstDTO.setCheck(false);
            data.add(addTreeData(maintenanceFirstDTO, 0));
        }
        if (data.size() == 0) {
            mAdapter.setEmptyView(mEmptyView);
        } else {
            mAdapter.setNewData(data);
        }

    }

    private HxMaintenanceFirstDTO addTreeData(HxMaintenanceFirstDTO maintenanceFirstDTO, int level) {
        maintenanceFirstDTO.setLevel(level);
        maintenanceFirstDTO.setItemType(level);
        HxLogUtils.i(TAG + "--22222data222222--" + maintenanceFirstDTO.toString());
        List<HxMaintenanceFirstDTO> children = maintenanceFirstDTO.getChildren();
        if (children != null && children.size() > 0) {
            for (int i = 0; i < children.size(); i++) {
                HxMaintenanceFirstDTO firstDTO = children.get(i);
                firstDTO = addTreeData(firstDTO, level + 1);
                maintenanceFirstDTO.addSubItem(firstDTO);
            }
        }
        return maintenanceFirstDTO;
    }

    private void initWebView() {
    }


       /*  WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.loadUrl("file:///android_asset/maintenance.html");
       mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                return super.shouldOverrideUrlLoading(webView, s);
            }

            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                //界面加载完成，动态设置数据
                setDataEchartView("[18,9,10,14]", "[9,4,7,11]", "1");
            }
        });
    }

    private void setDataEchartView(String maxData, String minData, String title) {
        drawChart(
                '{"first":{"title":"巡检站点数量","legend":{"data":["区域1","区域2","区域3"]},"series":[[18,9,10,14],[13,14,13,8],[9,4,7,11]]},
                "second":{"title":"任务完成数量","legend":{"data":["区域1","区域2","区域3"]},"series":[[18,9,10,14],[13,14,13,8],[9,4,7,11]]},
                "progress":1}'
        );
        String body = "'{" + "\"first\"" + ":" + maxData + "," + "\"second\"" + ":" + minData + ",\"title\":" + "\"" + title + "\"}'";
        mWebView.loadUrl("javascript:drawChart(" + body + ")");
    }
    */
}
